home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9252 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.8 KB  |  72 lines

  1. Path: solon.com!not-for-mail
  2. From: johnb@pivotal-dm.ccmail.compuserve.com (John Bain)
  3. Newsgroups: comp.lang.c,comp.lang.c.moderated
  4. Subject: Integral promotion problem
  5. Date: 8 Mar 1996 17:04:26 -0600
  6. Organization: Pivotal Technologies Ltd.
  7. Sender: clc@solutions.solon.com
  8. Approved: clc@solutions.solon.com
  9. Message-ID: <4hqedq$1mj@solutions.solon.com>
  10. NNTP-Posting-Host: solutions.solon.com
  11. X-Newsreader: Forte Agent .99d/32.182
  12.  
  13. Hi,
  14.  
  15. I thought I understood integral promotion, but the following example has
  16. me puzzled.
  17.  
  18. --------------------
  19. #include <stdio.h>
  20.  
  21. unsigned short s = 0xFFFF;
  22.  
  23. int main(void)
  24. {
  25.     int i = ~s;
  26.  
  27.     printf ("%x %x\n", i, (int)~s);
  28.     
  29.     if (i)
  30.         printf("i - NonZero\n");
  31.     else
  32.         printf("i - Zero\n");
  33.  
  34.     if ((int)~s)
  35.         printf("(int)~s - NonZero\n");
  36.     else
  37.         printf("(int)~s - Zero\n");
  38.  
  39.     return 0;
  40. }
  41. --------------------
  42.  
  43. When compiled on an implementation with 32-bit 2's complement ints I get
  44. the following output:
  45.  
  46. -------------------
  47. ffff0000 ffff0000
  48. i - NonZero
  49. (int)~s - Zero
  50. ------------------
  51.  
  52. The hex values and the result of the first "if" are the "surprising"
  53. results I was expecting.  However, the result of the second "if" has me
  54. confused.
  55.  
  56. What am I missing?
  57.  
  58. (In case it's relevent, the implementation defines the conversion of an
  59. unsigned integer to a signed integer, when the unsigned value cannot be
  60. represented in the signed integer, as being performed by truncation of
  61. the most significant bits if the signed integer is shorter, and then
  62. interpreting the remaining bits as a 2's complement integer.)
  63.  
  64. Cheers,
  65.  
  66. John
  67.  
  68. -----------------------------------------------------------------
  69. John Bain                  
  70. johnb@pivotal-dm.ccmail.compuserve.com 
  71. -----------------------------------------------------------------
  72.